/*-
 * ThreadTerminator.java
 *
 * $LastChangedBy: stein $
 * $LastChangedRevision: 37 $
 * $LastChangedDate: 2006-11-30 23:06:29 +0000 (Thu, 30 Nov 2006) $
 * $HeadURL: svn://localhost/SMS/src/com/googlecode/attention/sms/pdu/UDHI.java $
 *
 * Copyright (c) 2007, Stein Gunnar Bakkeby
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Stein Gunnar Bakkeby nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.attention.util;

/**
 * A thread terminator. This class is essentially a wrapper
 * for a boolean value used by threads that runs forever, but
 * should be able to stop when the boolean changes.
 *
 * @author Stein Gunnar Bakkeby
 * @param <OWNER> The owner object type
 */
public class ThreadTerminator<OWNER> {

  // TODO consider a Terminatable interface instead
  
  private boolean terminate = false;
  private final OWNER owner;
  
  /**
   * This will create the thread terminator without an owner.
   */
  public ThreadTerminator() {
    owner = null; // no owner
  }
  
  /**
   * This will create the thread terminator with a given owner,
   * only the owner can control whether to terminate other threads
   * given the terminator. 
   * 
   * @param owner The owner of this thread terminator
   */
  public ThreadTerminator(final OWNER owner) {
    this.owner = owner;
  }
  
  /**
   * Returns true if the thread must terminate, and false if the
   * thread should keep on running.
   * 
   * @return True if thread is terminating, false otherwise
   */
  public boolean isTerminating() {
    return terminate;
  }
  
  /**
   * This will terminate all threads using this thread terminator.
   */
  public void terminate() {
    if (owner != null) {
      // TODO check whether the calling object is infact the owner
      // also consider friends
    }
    terminate = true;
    // TODO should possibly wait a certain amount of
    // time here, and then call System.exit(0). Or perhaps not...
  }
  
  /**
   * Method for reporting that the thread has terminated.
   * 
   * @param object The runnable object that has terminated
   */
  public void hasTerminated(final Runnable object) {
    // TODO handle, also remove System.out.println below, no logging.
    System.out.println("Has terminated: " + object.toString());
  }
}
